package com.squirrelhuan.quickhttp.socket;

/**
 * 广告内容类型
 */
public enum ErrorCode {
    // 成功
    SUCCESS(0, "Success"),

    // 通用错误
    UNKNOWN_ERROR(1, "Unknown error occurred"),
    INVALID_ARGUMENT(2, "Invalid argument provided"),

    // TCP连接错误
    CONNECTION_REFUSED(100, "Connection refused by the target machine"),
    TIMEOUT(101, "Connection attempt timed out"),
    HOST_UNREACHABLE(102, "Host is unreachable"),
    SOCKET_NOT_CONNECTED(103, "Socket is not connected"),
    CONNECTION_RESET(104, "Connection was reset by the peer"),
    NETWORK_DOWN(105, "Network is down"),
    ALREADY_CONNECTED(106, "Already connected to the peer"),
    CONNECTION_ABORTED(107, "Software caused connection abort"),
    NO_ROUTE_TO_HOST(108, "No route to host"),
    ADDRESS_IN_USE(109, "Address already in use"),
    BUFFER_OVERFLOW(110, "Buffer overflow"),
    DNS_RESOLUTION_FAILED(111, "DNS resolution failed"),
    SSL_HANDSHAKE_FAILURE(112, "SSL handshake failed");

    private final int code;
    private final String description;

    ErrorCode(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("ErrorCode{code=%d, description='%s'}", code, description);
    }

    public static ErrorCode getErrorCode(int code) {
        ErrorCode resultEnum = null;
        ErrorCode[] enumArray = ErrorCode.values();
        for (ErrorCode errorCode : enumArray) {
            if (errorCode.code == code) {
                resultEnum = errorCode;
                break;
            }
        }
        return resultEnum;
    }
}
